package com.open4cn.simulator.core.util;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-12 23:49
 */
public final class StrFillUtil {
    private StrFillUtil() {
    }


    /**
     * 填充到左边
     *
     * @param source
     * @param maxLength
     * @param fill
     * @return
     */
    public static String fillLeft(String source, int maxLength, String fill) {
        if (null == fill ||
                null == source || source.length() >= maxLength) {
            return source;
        }
        while (source.length() < maxLength) {
            source = fill + source;
        }
        return source.length() > maxLength ? source.substring(0, maxLength) : source;
    }


    /**
     * 填充到右边
     *
     * @param source
     * @param maxLength
     * @param fill
     * @return
     */
    public static String fillRight(String source, int maxLength, String fill) {
        if (null == fill ||
                null == source || source.length() >= maxLength) {
            return source;
        }
        while (source.length() < maxLength) {
            source = source + fill;
        }
        return source.length() > maxLength ? source.substring(0, maxLength) : source;
    }


    /**
     * 补0左边
     *
     * @param needLength 总长度
     */
    public static String supplementZeroStr(String str, int needLength) {
        int d = needLength - str.length();
        String zeroStr = "";
        for (int i = 0; i < d; i++) {
            zeroStr += "0";
        }
        return zeroStr + str;
    }


}
